///*
// * Copyright © OpenAtom Foundation.
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *      http://www.apache.org/licenses/LICENSE-2.0
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// * See the License for the specific language governing permissions and
// * limitations under the License.
// *
// */
//
//package io.iec.edp.caf.msu.client.utils;
//
//import com.alibaba.nacos.api.exception.NacosException;
//import com.alibaba.nacos.api.naming.NamingService;
//import com.alibaba.nacos.api.naming.pojo.Instance;
//import io.iec.edp.caf.commons.exception.CAFRuntimeException;
//import io.iec.edp.caf.commons.exception.entity.DefaultExceptionProperties;
//import io.iec.edp.caf.commons.exception.ExceptionLevel;
//import io.iec.edp.caf.msu.api.enums.DeployMode;
//import io.iec.edp.caf.msu.api.entity.MsuProperties;
//import io.iec.edp.caf.msu.client.exception.MsuNotFoundException;
//import io.iec.edp.caf.rpc.server.invoker.RpcRemoteInvoker;
//import lombok.extern.slf4j.Slf4j;
//import lombok.var;
//
//
//import javax.servlet.http.Cookie;
//
//
//import java.util.HashMap;
//
///**
// * * 使用RPC与Server交互的工具类
// *
// * @author Leon Huo
// */
//@Slf4j
//@Deprecated
//public class MsuServerInvoker {
//
//    private static final int DEFAULT_PORT = 5200;
//
//    private RpcRemoteInvoker rpcRemoteInvoker;
//    private NamingService namingService;
//    private MsuProperties configuration;
//    private final String MSU_SERVICE_NAME = "MicroServiceUnit";
//
//
//    public MsuServerInvoker(RpcRemoteInvoker rpcRemoteInvoker, NamingService namingService, MsuProperties configuration) {
//        this.rpcRemoteInvoker = rpcRemoteInvoker;
//        this.namingService = namingService;
//        this.configuration = configuration;
//    }
//
//    public <T> T remoteInvoke(Class<T> clazz, String serviceId, HashMap<String, Object> parameters) {
//        String address = getAddress();
//        var eventContext = new HashMap<String,String>();
//        try {
//            log.info("当前寻找serviceName时的address:"+address);
//            return (T) rpcRemoteInvoker.remoteInvokeByAddress(clazz, serviceId, address, parameters, new Cookie[]{}, null,eventContext);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new MsuNotFoundException();
//        }
//    }
//
//    /**
//     * 获取Msu服务的地址
//     * @return
//     */
//    private String getAddress() {
//        String address;
//        if (configuration.getDeployMode() == DeployMode.OnPremise) {
//            Instance instance = null;
//            try {
//                instance = namingService.selectOneHealthyInstance(MSU_SERVICE_NAME);
//
//                log.info("当前MSU发现路由到MicroServiceUnit时的ip是："+instance.getIp());
//           } catch (NacosException e) {
//                throw new CAFRuntimeException(DefaultExceptionProperties.SERVICE_UNIT,
//                        DefaultExceptionProperties.RESOURCE_FILE,
//                        "Code todo",
//                        new String[]{},
//                        e, ExceptionLevel.Error, false);
//            }
//
//            address = instance.getIp() + ':' + instance.getPort();
//        } else {
//            address = MSU_SERVICE_NAME + ":" + DEFAULT_PORT;
//        }
//
//        return "http://" + address;
//    }
//}
